package mbp.gui.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import mbp.base.GuiFunction;
import mbp.core.ResourceValidation;
import mbp.gui.base.IListPanel;

import javax.swing.JButton;
import java.awt.Font;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;



public class ResourceValidationEditPanel extends JPanel implements ActionListener
{
	
	private ResourceValidation resourceValidation;
	private JTextField txtCondition;
	private JTextField txtType;
	private JButton btnSave;
	private IListPanel listPanel;
	private JTextField txtIntValue;

	/**
	 * Create the panel.
	 */
	private ResourceValidationEditPanel() 
	{
		setBorder(new TitledBorder(null, "Resource type and condition", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(null);
		
		btnSave = new JButton("Set");
		btnSave.setName("ACC_SET");
		btnSave.setFont(new Font("Dialog", Font.PLAIN, 11));
		btnSave.setBounds(12, 113, 80, 23);
		add(btnSave);
		btnSave.addActionListener(this);
		
		txtCondition = new JTextField();
		txtCondition.setBounds(12, 81, 270, 20);
		add(txtCondition);
		
		JLabel label = new JLabel("Condition");
		label.setFont(new Font("Dialog", Font.PLAIN, 12));
		label.setBounds(12, 67, 80, 14);
		add(label);
		
		txtType = new JTextField();
		txtType.setEditable(false);
		txtType.setBounds(12, 41, 120, 20);
		add(txtType);
		txtType.setColumns(10);
		
		JLabel lblType = new JLabel("Type");
		lblType.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblType.setBounds(12, 27, 70, 14);
		add(lblType);
		
		txtIntValue = new JTextField();
		txtIntValue.setBounds(142, 41, 86, 20);
		add(txtIntValue);
		txtIntValue.setColumns(10);
		
		JLabel lblAdditional = new JLabel("Extra value");
		lblAdditional.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblAdditional.setBounds(142, 28, 86, 14);
		add(lblAdditional);

	}
	
	public ResourceValidationEditPanel(GuiFunction guiFunction, IListPanel listPanel)
	{
		this();
		this.listPanel = listPanel;
	}
	
	public void LoadResourceValidation(ResourceValidation rvr)
	{
		this.txtType.setText(rvr.getType().getResourceTypeName());
		this.txtIntValue.setText(rvr.integerValue.toString());
		this.txtCondition.setText(rvr.getCondition());
		this.resourceValidation = rvr;
	}
	
	public void clear()
	{
		this.txtType.setText("");
		this.txtCondition.setText("");
		this.resourceValidation = null;	
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnSave)
		{
			if(resourceValidation != null)
			{
				resourceValidation.setCondition(txtCondition.getText());
				resourceValidation.integerValue = Integer.parseInt(this.txtIntValue.getText());
				if(listPanel != null)
				{
					listPanel.reloadList();
				}
			}
		}
		
	}
}
